﻿using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

using Aspects.PInvoke;

namespace Aspects.Panels
{
    public class ApplicationButton : Control
    {
        public ApplicationButton()
        {
            InitializeComponent();
        }

        public string ProgramLocation
        {
            get
            {
                if (programInfo == null)
                    return null;
                else
                    return programInfo.FullName;
            }
            set
            {
                if (value != null)
                {
                    try
                    {
                        programInfo = new FileInfo(value);
                        if (!programInfo.Exists)
                        {
                            this.Enabled = false;
                        }
                        Invalidate();
                        
                    }
                    catch
                    {
                        programInfo = null;
                        this.Enabled = false;
                    }
                }
                else
                {
                    programInfo = null;
                    Invalidate();
                }
            }
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            //draw raised 3d border
            e.Graphics.DrawLine(Pens.White,0,0,Width-2, 0);
            e.Graphics.DrawLine(Pens.LightGray, 1, 1, Width-3, 1);

            e.Graphics.DrawLine(Pens.White,0,0,0,Height-2);
            e.Graphics.DrawLine(Pens.LightGray, 1, 1, 1, Height-3);
            
            e.Graphics.DrawLine(Pens.Black,Width-1, 0, Width-1, Height-1);
            e.Graphics.DrawLine(Pens.DarkGray, Width-2, 1, Width-2, Height-2);
            
            e.Graphics.DrawLine(Pens.Black,0, Height-1, Width-1, Height-1);
            e.Graphics.DrawLine(Pens.DarkGray, 1, Height-2, Width-2, Height-2);
            
            if (Enabled)
            {
                var ico = Icon.ExtractAssociatedIcon(ProgramLocation);
                e.Graphics.DrawIcon(ico, new Rectangle(2,2, 15, 15));
                var brush = new SolidBrush(this.ForeColor);
                e.Graphics.DrawString(Text, this.Font, brush, new PointF(17.0f, 1.0f));
            }

        }
        
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // ApplicationButton
            // 
            this.Font = new System.Drawing.Font("Arial Narrow", 8.25F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));
            this.Name = "ApplicationButton";
            this.Size = new System.Drawing.Size(95, 22);
            this.Click += new System.EventHandler(this.ApplicationButton_Click);
            this.MouseEnter += new System.EventHandler(this.ApplicationButton_MouseEnter);
            this.MouseLeave += new System.EventHandler(this.ApplicationButton_MouseLeave);
            this.ResumeLayout(false);
        }

        #endregion
        
        private FileInfo programInfo;
        
        #region Control Events

        private void ApplicationButton_MouseEnter(object sender, EventArgs e)
        {
            if (this.Enabled)
            {
                this.BackColor = System.Drawing.SystemColors.Highlight;
                this.ForeColor = System.Drawing.SystemColors.HighlightText;
            }
        }

        private void ApplicationButton_MouseLeave(object sender, EventArgs e)
        {
            if (this.Enabled)
            {
                this.BackColor = System.Drawing.SystemColors.Control;
                this.ForeColor = System.Drawing.SystemColors.ControlText;
            }
        }

        private void ApplicationButton_Click(object sender, EventArgs e)
        {
            if (this.Enabled)
            {
                if (programInfo != null)
                {
                    Process.Start(programInfo.FullName);
                }
            }
        }

        #endregion
    }
}
